package com.orkasgb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orkasgb.entity.OrkasgbProcessEntity;
import com.orkasgb.entity.OrkasgbSubProcessEntity;
import com.orkasgb.mapper.OrkasgbProcessMapper;
import com.orkasgb.mapper.OrkasgbSubProcessMapper;
import com.orkasgb.service.IOrkasgbProcessService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 流程信息表(TOrkasgbProcess)表服务实现类
 *
 * @version 1.0.0
 * @since 2024-09-08 21:17:22
 */
@Slf4j
@Service
public class OrkasgbProcessServiceImpl extends ServiceImpl<OrkasgbProcessMapper, OrkasgbProcessEntity> implements IOrkasgbProcessService {
	
	@Resource
	private OrkasgbProcessMapper orkasgbProcessMapper;
	
	@Resource
	private OrkasgbSubProcessMapper orkasgbSubProcessMapper;
	
	/**
	 * 通过主键查询单条数据
	 *
	 * @param id 主键
	 * @return 单条数据
	 */
	@Override
	public OrkasgbProcessEntity getOrkasgbProcess (Serializable id) {
		return this.getBaseMapper().selectById(id);
	}
	
	/**
	 * 通过条件查询多条数据
	 *
	 * @param orkasgbProcessEntity 实体查询参数
	 * @return 数据
	 */
	@Override
	public List<OrkasgbProcessEntity> getOrkasgbProcessList (OrkasgbProcessEntity orkasgbProcessEntity) {
		QueryWrapper<OrkasgbProcessEntity> queryWrapper = new QueryWrapper<>(OrkasgbProcessEntity.class);
		return this.getBaseMapper().selectList(queryWrapper);
	}
	
	/**
	 * 通过条件查询多条数据(分页查询)
	 *
	 * @param orkasgbProcessEntity 查询参数实体
	 * @return 数据
	 */
	@Override
	public List<OrkasgbProcessEntity> getOrkasgbProcessPageDataList (OrkasgbProcessEntity orkasgbProcessEntity) {
		QueryWrapper<OrkasgbProcessEntity> queryWrapper = new QueryWrapper<>(OrkasgbProcessEntity.class);
		IPage<OrkasgbProcessEntity> page = new Page<>(1, 10);
		List<OrkasgbProcessEntity> orkasgbProcessList = this.getBaseMapper().selectList(page, queryWrapper);
		if (orkasgbProcessList.isEmpty()) {
			return orkasgbProcessList;
		}
		List<Long> processIdList = orkasgbProcessList.stream().map(OrkasgbProcessEntity::getId).collect(Collectors.toList());
		if (processIdList.isEmpty()) {
			return orkasgbProcessList;
		}
		List<OrkasgbSubProcessEntity> orkasgbSubProcessList =
				orkasgbSubProcessMapper.selectList(new LambdaQueryWrapper<OrkasgbSubProcessEntity>().in(OrkasgbSubProcessEntity::getProcessId, processIdList));
		Map<Long, List<OrkasgbSubProcessEntity>> mapSubProcess = orkasgbSubProcessList.stream().collect(Collectors.groupingBy(OrkasgbSubProcessEntity::getProcessId));
		orkasgbProcessList.forEach(process -> process.setChildren(mapSubProcess.get(process.getId())));
		return orkasgbProcessList;
	}
	
	/**
	 * 新增数据
	 *
	 * @param orkasgbProcessEntity 实体对象
	 * @return 新增结果
	 */
	@Override
	public int addOrkasgbProcess (OrkasgbProcessEntity orkasgbProcessEntity) {
		return this.getBaseMapper().insert(orkasgbProcessEntity);
	}
	
	/**
	 * 修改数据
	 *
	 * @param orkasgbProcessEntity 实体对象
	 * @return 修改结果
	 */
	@Override
	public int updateOrkasgbProcess (OrkasgbProcessEntity orkasgbProcessEntity) {
		return this.getBaseMapper().updateById(orkasgbProcessEntity);
	}
	
	/**
	 * 修改或者新增数据
	 *
	 * @param orkasgbProcessEntityList 实体对象
	 * @return 操作结果
	 */
	@Override
	public int addOrUpdateOrkasgbProcess (List<OrkasgbProcessEntity> orkasgbProcessEntityList) {
		return orkasgbProcessMapper.insertOrUpdateBatch(orkasgbProcessEntityList);
	}
	
	/**
	 * 删除数据
	 *
	 * @param idList 主键结合
	 * @return 删除结果
	 */
	@Override
	public int deleteOrkasgbProcessByIds (List<Long> idList) {
		return this.getBaseMapper().deleteByIds(idList);
	}
	
}

